package cn.easier.activity.console.controller;

import cn.easier.activity.console.configration.ActivityConsoleConfiguration;
import cn.easier.activity.console.repository.ActivityInfoRepository;
import cn.easier.activity.console.repository.ActivityWechatConfigRepository;
import cn.easier.activity.console.utils.FileUtil;
import cn.easier.activity.web.constant.WechatConstant;
import cn.easier.activity.web.entity.ActivityWechatConfig;
import cn.easier.club.base.dto.ResultInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@RequestMapping("/wechatConfig")
public class WechatConfigController extends BaseController {

    Logger logger = LoggerFactory.getLogger(WechatConfigController.class);
    private final ActivityConsoleConfiguration configuration;
    private final ActivityWechatConfigRepository wechatConfigRepository;
    private final ActivityInfoRepository activityInfoRepository;

    /**
     * 应用路径
     */
    @Value("${metadata.configuration.application-path}")
    public String applicationPath;

    @Autowired
    public WechatConfigController(ActivityConsoleConfiguration configuration, ActivityWechatConfigRepository wechatConfigRepository, ActivityInfoRepository activityInfoRepository) {
        this.configuration = configuration;
        this.wechatConfigRepository = wechatConfigRepository;
        this.activityInfoRepository = activityInfoRepository;
    }

    @GetMapping("/wechatList")
    public ResultInfo getWechatList() {
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        for (Map.Entry<String, String> entry : WechatConstant.TITLE_MAP.entrySet()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("key", entry.getKey());
            map.put("value", entry.getValue());
            list.add(map);
        }
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "", list);
    }

    @GetMapping("/{activityId}")
    public ResultInfo getWechatConfig(@PathVariable("activityId") Integer activityId) {
        AtomicReference<ActivityWechatConfig> wechatConfig = new AtomicReference<>();
        wechatConfigRepository.findByActivityId(activityId).ifPresent(wechatConfig::set);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "", wechatConfig.get());
    }

    @PostMapping("/save")
    public ResultInfo remark(@RequestBody ActivityWechatConfig activityWechatConfig) {
        try {
            Integer activityId = activityWechatConfig.getActivityId();
            if (activityId != null) {
                activityInfoRepository.findById(activityId).ifPresent(activityInfo -> activityWechatConfig.setShareUrl(configuration.foregroundStaticLink + "?activityId=" + activityId + "&typeIdentification=" + activityInfo.getTypeIdentification()));
                AtomicReference<Integer> id = new AtomicReference<>(null);
                wechatConfigRepository.findByActivityId(activityId).ifPresent(config -> id.set(config.getId()));
                activityWechatConfig.setId(id.get());
                wechatConfigRepository.save(activityWechatConfig);
            } else {
                throw new RuntimeException();
            }
        } catch (Exception e) {
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "保存失败", null);
        }
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "保存成功", null);
    }


    @PostMapping("/imageUpload")
    public ResultInfo imageUpload(MultipartFile file, String prefix) throws InterruptedException {
        Thread.sleep(3000);
        String url = FileUtil.doFile(file, configuration.realPath + configuration.wechatConfigImagePath, prefix);
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "上传完成", configuration.wechatConfigImagePath + url);
    }


}